﻿using FastORM.Entity;
using FastORM.Enum;
using FastORM.Handle;
using FastORM.Mapping;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace FastORM.Applicaiton
{
    public class CommonRow
    {
        public string ConnectionString { set; get; }
        public DataBaseClass DataBaseClass { set; get; }
        public Hashtable RowData { set; get; }
        public string TableName { set; get; }
        private string KeyFiledName { set; get; }
        public CommonRow(string TableName, string KeyFiledName, DataBaseClass DataBaseClass,string ConnectionString)
        {
            this.ConnectionString = ConnectionString;
            this.DataBaseClass = DataBaseClass;
            this.TableName = TableName;
            this.KeyFiledName = KeyFiledName;
            RowData = new Hashtable();
            RowData.Add(KeyFiledName, GenerateUtil.GenerateUnitGuid());
        }
        public CommonRow(string TableName, string KeyFiledName, string KeyFieldValue, DataBaseClass DataBaseClass,string ConnectionString)
        {
            this.ConnectionString = ConnectionString;
            this.DataBaseClass = DataBaseClass;
            this.TableName = TableName;
            this.KeyFiledName = KeyFiledName;
            RowData = new Hashtable();
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            DataTable dt = Session.Find(TableName, KeyFiledName, KeyFieldValue);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                foreach (DataColumn column in row.Table.Columns)
                {
                    RowData.Add(column.ColumnName, row[column.ColumnName]);
                }
            }
            else
            {
                RowData.Add(KeyFiledName, KeyFieldValue);
            }
        }


        public object this[string ColumnName]
        {
            set
            {
                RowData[ColumnName] = value;
            }
            get
            {
                return RowData[ColumnName];
            }
        }

        public bool Exist()
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            string sql = string.Format("select count(1) from {0} where {1}='{2}'", TableName, KeyFiledName, ValueConvert.ConvertToString(RowData[KeyFiledName]));
            int count = ValueConvert.ConvertToInt32(Session.ExecuteScar(sql));
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Insert()
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Insert(TableName, RowData);
        }
        public void Update()
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Update(TableName, KeyFiledName, RowData);
        }
        public void Delete()
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Delete(TableName, KeyFiledName,RowData[KeyFiledName]);
        }

    }
}
